$(function () {
    var tp = js.initTabPage("tabpanel", {
        height: function () {
            var g = $(window).height(), e = $(".main-header:visible").outerHeight(),
                f = $(".main-footer:visible").outerHeight(), d = g - e - f;
            return d < 300 ? 300 : d
        }
    });


    $(window).resize();

    var b = $("#desktopTabPage");
    if (b.size() > 0) {
        var a = b.data("url");
        if (a && a != "" ) {
            js.addTabPage(null, '<i class="fa fa-home"></i> ' + b.data("title"), a, false, false)
        }
    }

    $(window).bind("hashchange", function (g) {
        controlTab();
    });

    $(".sidebar-toggle").click(function(){
        tp.resize();
	});
});


//生成菜单
var menuItem = Vue.extend({
	name: 'menu-item',
	props:{item:{}},
	template:[
	          '<li>',
	          '<a v-if="item.type === 0" href="javascript:;">',
	          '<i v-if="item.icon != null" class="fa fa-fw" :class="item.icon"></i>',
	          '<span> {{item.name}}</span>',
	          '<i class="fa fa-angle-left pull-right"></i>',
	          '</a>',
	          '<ul v-if="item.type === 0" class="treeview-menu">',
	          '<menu-item :item="item" v-for="(item,index) in item.list" :key="index"></menu-item>',
	          '</ul>',
	          '<a v-if="item.type === 1" :href="\'#\'+item.url" class="addTabPage" >&nbsp;&nbsp;<i v-if="item.icon != null"  class="fa fa-fw" :class="item.icon"></i><i v-else class="fa fa-circle-o"></i> {{item.name}}</a>',
	          '</li>'
	].join('')
});

// var tabpanel =  new TabPanel({
//     renderTo:'tab',
//     width: 800,
//     height: 400,
//     //border:'none',
//     active : 0,
//     //maxLength : 10,
//     items : [
//         {id:'toolbarPlugin1',title:'首页',html:jcTabs[0],closable: false}
//     ]
// });

//注册菜单组件
Vue.component('menuItem',menuItem);

var vm = new Vue({
	el:'#rrapp',
	data:{
		user:{},
		menuList:{},
		main:"sys/main.html",
		password:'',
		newPassword:'',
        navTitle:"控制台",
		updatedFlag:false
	},
	methods: {
		getMenuList: function (event) {
			$.getJSON("sys/menu/user?_"+$.now(), function(r){
				vm.menuList = r.menuList;
			});
		},
		getUser: function(){
			$.getJSON("sys/user/info?_"+$.now(), function(r){
				vm.user = r.user;
			});
		},
		updatePassword: function(){
			layer.open({
				type: 1,
				skin: 'layui-layer-lan',
				title: "修改密码",
				area: ['550px', '270px'],
				shadeClose: false,
				content: jQuery("#passwordLayer"),
				btn: ['修改','取消'],
				btn1: function (index) {
					var data = "password="+vm.password+"&newPassword="+vm.newPassword;
					$.ajax({
						type: "POST",
					    url: "sys/user/password",
					    data: data,
					    dataType: "json",
					    success: function(result){
							if(result.code == 0){
								layer.close(index);
								layer.alert('修改成功', function(index){
									location.reload();
								});
							}else{
								layer.alert(result.msg);
							}
						}
					});
	            }
			});
		}
	},
	created: function(){
		this.getMenuList();
		this.getUser();
	},
	updated: function(){
		if(this.updatedFlag){
            controlTab();
		}
        this.updatedFlag = true;
	}
});



function controlTab() {
    var url = window.location.hash;
    var f = url.replace("#", "");
    var tabId = $("a[href='"+url+"']").val("tab-id");
    if (f && f != "" && f != window.location.pathname&&tabId&&tabId!="") {
        //导航菜单展开
        $(".treeview-menu li").removeClass("active");
        $("a[href='"+url+"']").parents("li").addClass("active");
        tabId = js.addTabPage($("a[href='"+url+"']"), $("a[href='"+url+"']").text().replace(/&nbsp;/ig,''), f);
        $("a[href='"+url+"']").attr("tab-id",tabId);
    } else {
        $(".sidebar-menu > li:eq(0):not(.active) > a:eq(0)").click()
    }
}

function routerList(router, menuList){
	for(var key in menuList){
		var menu = menuList[key];
		if(menu.type == 0){
			routerList(router, menu.list);
		}else if(menu.type == 1){
			router.add('#'+menu.url, function() {
				var url = window.location.hash;
				
				//替换iframe的url
			    vm.main = url.replace('#', '');
			    
			    //导航菜单展开
			    $(".treeview-menu li").removeClass("active");
			    $("a[href='"+url+"']").parents("li").addClass("active");
			    
			    vm.navTitle = $("a[href='"+url+"']").text();
			});
		}
	}
}


